package com.example.androidapp2;

import com.example.androidapp2.DbClasses.ItemCategoria;
import com.example.androidapp2.DbClasses.ItemIngresos;
import com.example.androidapp2.DbClasses.Utils;
import com.xyzdesa.droidnomy.R;
import android.app.Dialog;
import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Spinner;

public class PopUpModifica extends Dialog  {
	DbAdapter DbConect;
	Context Ctx;

	public EditText txtSueldo;	
	public Button btnGuardar;
	public Button btnEliminar;	
	public CheckBox chbAutomatico;	
	public Spinner spnCate;	
	public DatePicker dtpFecha;
	public CheckBox chbMedio;	
	public RelativeLayout lytMedio;
	public ItemIngresos Itm;
	public Boolean EsIngreso = false;

	public PopUpModifica(Context context, DbAdapter db) {
		super(context);
		Ctx = context;
		DbConect = db;

		this.setContentView(R.layout.pup_modif);		

		txtSueldo = (EditText) findViewById(R.id.txtSueldo);		
		btnGuardar = (Button) findViewById(R.id.btnGuardar);
		btnEliminar = (Button)findViewById(R.id.btnBorrar);
		chbMedio = (CheckBox) findViewById(R.id.chbMedio);
		dtpFecha = (DatePicker) findViewById(R.id.dtpFecha);
		chbAutomatico = (CheckBox) findViewById(R.id.chbAutomatico);
		lytMedio = (RelativeLayout) findViewById(R.id.lytMedio);
		spnCate = (Spinner) findViewById(R.id.spnCategorias);		
	}	

	public void show(ItemIngresos itm)
	{
		this.setTitle(R.string.pupModifica);
		Itm = itm;
		spnCate.setVisibility(View.VISIBLE);
		lytMedio.setVisibility(View.VISIBLE);	
		dtpFecha.setVisibility(View.VISIBLE);
		btnEliminar.setVisibility(View.VISIBLE);
		if(itm.Categoria!=null)
			chbAutomatico.setVisibility(View.INVISIBLE);
		SetListeners();


		txtSueldo.setText(String.valueOf(itm.Valor));
		String[] fecha = itm.Fecha.split("/");
		dtpFecha.updateDate(Integer.parseInt(fecha[2]),Integer.parseInt(fecha[1])-1,Integer.parseInt(fecha[0]));

		if(itm.Categoria!=null){			

			for (int i = 0; i < spnCate.getCount(); i++) {
				if(itm.Categoria.Codigo == ((ItemCategoria)spnCate.getItemAtPosition(i)).Codigo){
					spnCate.setSelection(i);				
					break;
				}
			}		
			chbMedio.setChecked((itm.Medio.equals(Utils.medioCred)));
		}else{
			spnCate.setVisibility(View.INVISIBLE);
			lytMedio.setVisibility(View.INVISIBLE);
		}

		if(DbConect.GetIngresoAutomatico()==itm.Valor)
			chbAutomatico.setChecked(true);

		RelativeLayout rlt = (RelativeLayout) findViewById(R.id.lytBoton);
		RelativeLayout.LayoutParams pr = new RelativeLayout.LayoutParams(rlt.getLayoutParams());
		pr.addRule(RelativeLayout.BELOW,R.id.lytMedio);
		rlt.setLayoutParams(pr);

		this.show();
	}

	public void show(boolean esIngreso)
	{
		this.setTitle(R.string.pupIncomes);
		this.EsIngreso = esIngreso;
		spnCate.setVisibility(View.INVISIBLE);
		lytMedio.setVisibility(View.INVISIBLE);
		dtpFecha.setVisibility(View.INVISIBLE);	
		btnEliminar.setVisibility(View.INVISIBLE);
		chbAutomatico.setVisibility(View.VISIBLE);

		RelativeLayout rlt = (RelativeLayout) findViewById(R.id.lytBoton);
		RelativeLayout.LayoutParams pr = new RelativeLayout.LayoutParams(rlt.getLayoutParams());
		pr.addRule(RelativeLayout.BELOW,R.id.chbAutomatico);
		rlt.setLayoutParams(pr);

		txtSueldo.setText("");
		chbAutomatico.setChecked(false);
		SetListeners();

		this.show();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode ==  KeyEvent.KEYCODE_BACK && (dtpFecha.getVisibility() == View.VISIBLE || EsIngreso == true))			
			this.hide();

		return true;
	}	

	public void SetListeners()
	{
		btnGuardar.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if(dtpFecha.getVisibility() == View.INVISIBLE){
					DbConect.InsertMovimiento(Integer.parseInt(txtSueldo.getText().toString()), "In", Utils.GetFecha(), Utils.medioEft, 0,1);					
				}
				else{
					if(Itm.Categoria!=null)
						DbConect.UpdateMovimientos(Itm.Id, Integer.parseInt(txtSueldo.getText().toString()), ((ItemCategoria)spnCate.getSelectedItem()).Codigo, (chbMedio.isChecked())?Utils.medioCred:Utils.medioEft,Utils.GetFecha(dtpFecha.getDayOfMonth(), dtpFecha.getMonth()+1, dtpFecha.getYear()));
					else
						DbConect.UpdateMovimientos(Itm.Id, Integer.parseInt(txtSueldo.getText().toString()), 0, Utils.medioEft,Utils.GetFecha(dtpFecha.getDayOfMonth(), dtpFecha.getMonth()+1, dtpFecha.getYear()));
					if(Itm.Medio.equals(Utils.medioEft) && chbMedio.isChecked())
						((MainActivity)Ctx).pupTarjeta.show(Itm.Id);
					else if (Itm.Medio.equals(Utils.medioCred) && !chbMedio.isChecked()){
						DbConect.DeleteMovimientosPagos(Itm.Id);
						DbConect.DeleteTarjetasPorIdMovimiento(Itm.Id);
					}
				}	

				((MainActivity)Ctx).CargarGrilla(DbConect.GetIngresos());
				((MainActivity)Ctx).ActualizarProgressBar();
				hide();
			}
		});

		btnEliminar.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				((MainActivity)Ctx).pupConfirma.show("EliminarMov", new int[]{Itm.Id});				 
			}
		});

		ArrayAdapter<ItemCategoria> spinnerAdapter = new ArrayAdapter<ItemCategoria>(this.Ctx,android.R.layout.simple_spinner_item, DbConect.GetCategorias("spnCategorias",false,false));
		spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);		
		spnCate.setAdapter(spinnerAdapter);	
	}
}
